﻿using System;
using System.Collections.Generic;
using System.Text;

namespace csharpcore7_ch3
{
    /// <summary>
    /// 协变
    /// </summary>
    class GenericExample3
    {
        static void _Main()
        {

            var bears = new Stack<Bear>();
            //Stack<Animal> animals = bears; //编译时错误，可变性不是自动的

            //ZooCleaner.Wash(bears);//编译时错误，可变性不是自动的

            ZooCleaner.Wash1(bears);

            Bear[] bearsArray = new Bear[3];
            Animal[] animalsArray = bearsArray;   //语法上没问题,但当类型不一致时，会报错
            //animalsArray[0] = new Camel();  //运行时错误
        }
    }
    public class Animal { }
    public class Bear : Animal { }
    public class Camel : Animal { }
    class ZooCleaner
    {
        public static void Wash(Stack<Animal> animals) {
            Console.WriteLine("清理动物园！");
        }
        /// <summary>
        /// 带有约束的Wash方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="animals"></param>
        public static void Wash1<T>(Stack<T> animals) where T :Animal
        {
            Console.WriteLine("清理动物园1！");
        }

        public static void Wash2(IPopable<Animal> animals)
        {
            Console.WriteLine("清理动物园1！");
        }
    }
}
